#include "scanner.hpp"
#include "functions.hpp"
//scans for #include or .include and includes the files
bool inc_scanner(vector<string>& file,int& line_amount) {
    //setup all the variables needed
    //it is the pointer to the start of the vector<string>
    vector<string>::iterator it = file.begin();
    //i is the current line
    int i = 0;
    //tmp a integer for temorary usage on readfile due to the fact that we don't actually need the second perameter
    int tmp;
	string s;
    //file2 is the vector<string> we need for getting the content of the second file
    vector<string> file2;
    while (++it!=file.end()) {
        //line is the current line being checked
        string line = *it;
        //get the first 8 characters
        line = tolowercase(line.substr(0,8));
        //check if they are one of the correct include statements
        if (line == "#include" || line == ".include") {
            //get's the whole line again
            fix_line_newline(line = *it);//,it-file.begin());
            //converts it to the correct variable format and strips everything but the path
			string sfile= line.substr(10,line.length()-10-(line.length()-line.find_last_of("\"")));
			string spath= filepath+sfile;
            const char* path = (spath).c_str();
            //alert the user on the fact that a file is being included
	    write("Including file "+line.substr(10,line.length()-10-(line.length()-line.find_last_of("\""))),"32");
            //clear the include file's content from the last include
            file2.clear();
            //read the file
            if (!read_file(file2, tmp, path)){
				write((string)path+" can not be found","31",true);
				return(false);
			}
			//insert the contents of the file in the place of the #include statement
            insert_vs(file,file2,line_amount,i);
            //reset the pointer to the start of the file and re-scan for #include statements
            it = file.begin();
            //increase the line pointer
            i=0;
        }
        else
            //increast the line pointer
            i++;
    }
    return(true);
}
void commented_scanner(vector<string>& file,int& line_amount) {
    //setup all the variables needed
    //it is the pointer to the start of the vector<string>
    vector<string>::iterator it = file.begin();
    int x;
    //line is the temporary string for the current line
    string line;
    //scan through file
    while (++it!=file.end()) {
        //line is the current line being checked
        line = *it;
        //sets the temporary variable to 0
        x=0;
        //scans through for the first non-whitespace character
        while (line.substr(x,1)==" "||line.substr(x,1)=="\t")
            x++;
        //stores the first non-whitespace character to line
        line = line.substr(x,1);
        //checks to see if it is a comment
        if (line==";") {
            //erases the line if it is
            file.erase(it);
            //decreases the line amount
            line_amount--;
            //steps back one pointer location so as not to skip over lines
            --it;
        }
    }
}
void comments_scanner(vector<string>& file,int& line_amount) {
    //setup all the variables needed
    //it is the pointer to the start of the vector<string>
    vector<string>::iterator it = file.begin();
    int x;
    //line is the temporary string for the current line
    string line;
    //creates a string so that you know if it is inside a " or a '
    string delim = "\0";
    //creates a temporary string
    string tmp;
    //scan through file
    while (++it!=file.end()) {
        //line is the current line being checked
        line = *it;
        //sets the temporary variable to 0
        x=0;
        //scans for comments and removes them
        while(x!=(int)line.length()){
        	//scans for the first catch character
			while (line.substr(x,1)!=";" && line.substr(x,1)!="\"" && line.substr(x,1)!="'" && x != (int)line.length())
				x++;
			//check to make sure it didn't halt just because it is the last character
			if( x != (int)line.length() && (line.substr(x,1)==";" || line.substr(x,1)=="\"" || line.substr(x,1)=="'")){
				//checks to see if the delimiter is set
				if (delim!="\0"){
					//makes sure that it isn't a literal
					if(line.substr(x,1)==delim && line.substr(x-1,1)!="\\"){
						//resets the delimiter
						delim="\0";
						//increases the line number to avoid infinity loop
						x++;
					}
				}
				else{
					//stores the detected character to tmp
					tmp = line.substr(x,1);
					//if tmp is not the comment character
					if(tmp!=";"){
						//set the delimiter
						delim = tmp;
						//increase the line number to avoid infinity loop
						x++;
					}
					else{
						//remove the comment
						line = line.substr(0,x);
						//erase the current line
						file.erase(it);
						//insert the de-commented line
						file.insert(it,line);
						//reset x to 0 so you can start searching from the start of the next line
						x=0;
					}
				}
			}
        }
    }
}
